package gov.nhso.prov.acc.apps.acc7;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.isud.core.ISUD;
import com.isud.core.ISUDFactory;

import gov.nhso.prov.acc.beans.Acc67ExpenseCategoryBean;
import gov.nhso.prov.acc.core.DBConnection;
import gov.nhso.prov.acc.utils.Utility;

@SuppressWarnings("rawtypes")
public class ACC70201 extends Utility{
	public String url = "/apps/acc7/ACC70201A.jsp";
	
	public ACC70201(HttpServletRequest request, HttpServletResponse response) {
		this.message(request).clearMsg();
		String job = this.getAttr(request, "job");
		if (job.equals("init")){
			this.init(request);
		} else if (job.equals("add")){
			this.add(request);
		} else if (job.equals("save")){
			this.save(request);
		} else if (job.equals("back")){
			this.init(request);
		} else if (job.equals("saveParent")){
			this.saveParent(request);
		} else if (job.equals("rollBack")){
			this.rollBack(request);
		} else if(job.equals("saveNagative")){
			this.saveNagative(request);
		}
	}
	private String init(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			List listData = new ArrayList();
			
			Acc67ExpenseCategoryBean expenseCategoryBean = new Acc67ExpenseCategoryBean();
			expenseCategoryBean.setCategoryAcc("7");
			listData = dao.selectList(expenseCategoryBean, " AND CATEGORY_SEQ IS NULL AND CATEGORY_LEVEL IS NULL ORDER BY CATEGORY_ID ASC");
			
			request.setAttribute("expenseCategoryBean", listData);
			
			listData = new ArrayList();
			expenseCategoryBean = new Acc67ExpenseCategoryBean();
			expenseCategoryBean.setCategoryAcc("7");
			listData = dao.selectList(expenseCategoryBean, " AND CATEGORY_SEQ IS NOT NULL AND CATEGORY_LEVEL IS NOT NULL ORDER BY CATEGORY_SEQ ASC");

			request.setAttribute("expenseCategoryBeanList", listData);
			request.setAttribute("job", "add");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String add(HttpServletRequest request){
		request.setAttribute("job", "save");
		this.url = "/apps/acc7/ACC70201B.jsp";
		return this.url;
	}
	private String save(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			
			String[] categoryName = request.getParameterValues("categoryName");
			String[] categoryDesc = request.getParameterValues("categoryDesc");
			
			Acc67ExpenseCategoryBean expenseCategoryBean = new Acc67ExpenseCategoryBean();
			
			if(categoryName.length > 0){
				for(int i = 0;i<categoryName.length;i++){
					expenseCategoryBean = new Acc67ExpenseCategoryBean();
					expenseCategoryBean.setCategoryAcc("7");
					expenseCategoryBean.setCategoryId(this.genCode(dao).getExpenseCategoryId());
					expenseCategoryBean.setCategoryName(this.encodeUTF(categoryName[i]));
					expenseCategoryBean.setCategoryDesc(this.encodeUTF(categoryDesc[i]));
					expenseCategoryBean.setCreatedBy(this.user(request).getUserId());
					expenseCategoryBean.setUpdatedBy(this.user(request).getUserId());
					dao.insert(expenseCategoryBean);
				}
			}
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.init(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String saveParent(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			
			String categoryId = this.getAttr(request, "categoryId");
			String categoryLevel = this.getAttr(request, "categoryLevel");
			String categorySeq = this.getAttr(request, "categorySeq");
			String[] arrCategoryId = request.getParameterValues("childId");
			Acc67ExpenseCategoryBean expenseCategoryBean = new Acc67ExpenseCategoryBean();
			Map map = new HashMap();
			List list = new ArrayList();
			int intCategoryLevel = 0;
			int maxSeq = 0;
			StringBuffer sb = new StringBuffer();
			
			if (categoryId.equals("0") && categoryLevel.equals("0")){
				sb.append("SELECT MAX(CATEGORY_SEQ) AS MAX_SEQ FROM "+this.schema+".ACC67_EXPENSE_CATEGORY ");
				sb.append("WHERE CATEGORY_ACC = '7'");
				map = dao.findDataSql(sb.toString());
				if (map.get("MAX_SEQ") != null && !map.get("MAX_SEQ").toString().equals("")) {
					maxSeq = Integer.parseInt(map.get("MAX_SEQ").toString())+1;
				}else {
					maxSeq = 1;
				}
				if (arrCategoryId.length > 0) {
					for(int i = 0 ;i<arrCategoryId.length;i++){
						expenseCategoryBean = new Acc67ExpenseCategoryBean();
						expenseCategoryBean.setCategoryId(arrCategoryId[i]);
						expenseCategoryBean.setCategoryLevel("1");
						expenseCategoryBean.setCategoryParent("0");
						expenseCategoryBean.setCategorySeq(String.valueOf(maxSeq));
						expenseCategoryBean.setUpdatedBy(this.user(request).getUserId());
						expenseCategoryBean.setUpdatedDate(this.sysDate);
						maxSeq += 1;
						dao.update(expenseCategoryBean);
					}
				}
			}else {
				intCategoryLevel = Integer.parseInt(categoryLevel) + 1;
				sb.append("SELECT CATEGORY_ID FROM "+this.schema+".ACC67_EXPENSE_CATEGORY ");
				sb.append("WHERE CATEGORY_SEQ > "+categorySeq+" AND CATEGORY_ACC = '7'  ORDER BY CATEGORY_SEQ ASC");
				list = dao.listDataSql(sb.toString());
				
				if (arrCategoryId.length > 0) {
					for(int i = 0 ;i<arrCategoryId.length;i++){
						expenseCategoryBean = new Acc67ExpenseCategoryBean();
						expenseCategoryBean.setCategoryId(arrCategoryId[i]);
						expenseCategoryBean.setCategoryParent(categoryId);
						expenseCategoryBean.setCategorySeq(String.valueOf(Integer.parseInt(categorySeq)+(i+1)));
						expenseCategoryBean.setCategoryLevel(String.valueOf(intCategoryLevel));
						expenseCategoryBean.setUpdatedBy(this.user(request).getUserId());
						expenseCategoryBean.setUpdatedDate(this.sysDate);
						maxSeq = Integer.parseInt(categorySeq)+(i+1);
						dao.update(expenseCategoryBean);
					}
				}
				if(list.size() > 0){
					for (int i = 0; i < list.size(); i++) {
						map = (Map)list.get(i);
						maxSeq += 1;
						expenseCategoryBean = new Acc67ExpenseCategoryBean();
						expenseCategoryBean.setCategoryId(map.get("CATEGORY_ID").toString());
						expenseCategoryBean.setCategorySeq(String.valueOf(maxSeq));
						expenseCategoryBean.setUpdatedBy(this.user(request).getUserId());
						expenseCategoryBean.setUpdatedDate(this.sysDate);
						dao.update(expenseCategoryBean);
					}
				}
			}
			
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.init(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String rollBack(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);

			String categoryId = this.getAttr(request, "categoryId");
			String categorySeq = this.getAttr(request, "categorySeq");
			Map map = new HashMap();
			List list = new ArrayList();
			StringBuffer sb = new StringBuffer();
			int maxSeq = 0;
			
			Acc67ExpenseCategoryBean expenseCategoryBean = new Acc67ExpenseCategoryBean();
			expenseCategoryBean.setCategoryId(categoryId);
			expenseCategoryBean.setCategoryLevel("null");
			expenseCategoryBean.setCategoryParent("null");
			expenseCategoryBean.setCategorySeq("null");
			expenseCategoryBean.setCategoryApply("N");
			expenseCategoryBean.setUpdatedBy(this.user(request).getUserId());
			expenseCategoryBean.setUpdatedDate(this.sysDate);
			dao.update(expenseCategoryBean, " OR CATEGORY_PARENT = '"+categoryId+"' AND CATEGORY_ACC = '7' ");
			

			sb.append("SELECT CATEGORY_ID,CATEGORY_SEQ FROM "+this.schema+".ACC67_EXPENSE_CATEGORY ");
			sb.append("WHERE CATEGORY_SEQ > "+categorySeq+" AND CATEGORY_ACC = '7' ORDER BY CATEGORY_SEQ");
			list = dao.listDataSql(sb.toString());

			maxSeq = Integer.parseInt(categorySeq);
			if(list.size() > 0){
				for (int i = 0; i < list.size(); i++) {
					map = (Map)list.get(i);
					expenseCategoryBean = new Acc67ExpenseCategoryBean();
					expenseCategoryBean.setCategoryId(map.get("CATEGORY_ID").toString());
					expenseCategoryBean.setCategorySeq(String.valueOf(maxSeq));
					expenseCategoryBean.setUpdatedBy(this.user(request).getUserId());
					expenseCategoryBean.setUpdatedDate(this.sysDate);
					dao.update(expenseCategoryBean);
					maxSeq++;
				}
			}
			
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.init(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String saveNagative(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);

			String[] apply = request.getParameterValues("apply");
			Acc67ExpenseCategoryBean expenseCategoryBean = new Acc67ExpenseCategoryBean();
			
			expenseCategoryBean.setCategoryApply("N");
			dao.update(expenseCategoryBean, " AND (1=1)");
			
			if (apply != null && apply.length > 0){
				for(int i=0 ;i<apply.length;i++){
					expenseCategoryBean = new Acc67ExpenseCategoryBean();
					expenseCategoryBean.setCategoryApply("Y");
					expenseCategoryBean.setCategoryId(apply[i]);
					expenseCategoryBean.setUpdatedBy(this.user(request).getUserId());
					expenseCategoryBean.setUpdatedDate(this.sysDate);
					dao.update(expenseCategoryBean);
				}
			}
			
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.init(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
}
